import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
import Main from "@/components/main/main";

/**
 meta: {
    hide: false, 是否在左侧菜单显示 不显示请设为 true
    title: "列表页面", 菜单标题
    icon: "md-grid", 图标
    roleId: 1 菜单权限id 不填写等同于不需要权限校验
    singlePage: true 单页 非嵌套栏目显示
}
 */

// 不需要权限校验的静态路由
export const constantRoutes = [
  {
    path: "/login",
    name: "login",
    meta: {
      hide: true,
      title: "登录",
    },
    component: () => import("@/views/login/login")
  },
  {
    path: "/",
    name: "Dashboard",
    component: Main,
    meta: {
      hide: false,
      title: "Dashboard",
    },
    children: [
      {
        path: "/workplace",
        name: "workplace",
        meta: {
          hide: false,
          title: "工作台"
        },
        component: () => import("@/views/Dashboard/workplace")
      },
    ]
  },
  {
    path: "/group_management",
    name: "group_management",
    component: Main,
    meta: {
      hide: false,
      title: "集团管理",
      singlePage: true
    },
    children: [
      {
        path: "/organization",
        name: "organization",
        meta: {
          hide: false,
          title: "部门管理"
        },
        component: () => import("@/views/group_management/organization/organization-search")
      },
      {
        path: "/position",
        name: "position",
        meta: {
          hide: false,
          title: "岗位管理"
        },
        component: () => import("@/views/group_management/position/position-search")
      },
      {
        path: "/staff",
        name: "staff",
        meta: {
          hide: false,
          title: "人员管理"
        },
        component: () => import("@/views/group_management/staff/staff-search")
      }
    ]
  },
  {
    path: "/settings",
    name: "settings",
    component: Main,
    meta: {
      hide: false,
      title: "系统设置",
      singlePage: true
    },
    children: [
      {
        path: "/permission",
        name: "permission",
        meta: {
          hide: false,
          title: "权限维护",
          singlePage: true // 单页
        },
        component: () => import("@/views/settings/permission/permission-search")
      },
      {
        path: "/dictionary",
        name: "dictionary",
        meta: {
          hide: false,
          title: "字典管理",
          singlePage: true // 单页
        },
        component: () => import("@/views/settings/dictionary/dictionary-search")
      },
      {
        path: "/roleSearch",
        name: "roleSearch",
        meta: {
          hide: false,
          title: "角色管理",
          singlePage: true // 单页
        },
        component: () => import("@/views/settings/role/role-search")
      },
      {
        path: "/clientList",
        name: "clientList",
        meta: {
          hide: false,
          title: "客户端管理",
          singlePage: true // 单页
        },
        component: () => import("@/views/settings/client/client-search")
      },
      {
        path: "/menu",
        name: "menu",
        meta: {
          hide: false,
          title: "菜单管理",
          singlePage: true // 单页
        },
        component: () => import("@/views/settings/menu/menu-search")
      },
      {
        path: "/personal",
        name: "personal",
        meta: {
          hide: false,
          title: "个人中心",
          singlePage: true // 单页
        },
        component: () => import("@/views/person/personal")
      }
    ]
  },
]

const createRouter = () => new Router({
  mode: 'history',
  routes: setRouterInfo(constantRoutes)
})

const setRouterInfo = (routers)=>{
  for(let v of routers) {
    if (v.children) {
      setFuName(v.children, v)
    }
    function setFuName(chid, v) {
      // 开始处理子级
      for (let k of chid) {
        k.meta.fuName = v.name
        k.meta.fuTitle = v.meta.title
      }
    }
  }
  return routers
}

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// 解决跳转同一个路由报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
